package com.example.demo02.utils;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class R {

    private Integer code;
    private String message;
    private Object data;

    private R() {}

    public static R ok() {
        R result = new R();
        result.setCode(200);
        result.setMessage("成功");
        return result;
    }

    public static R error() {
        R result = new R();
        result.setCode(500);
        result.setMessage("失败");
        return result;
    }

    public R data(String key, Object value) {
        this.data = new HashMap<>();
        ((Map) this.data).put(key, value);
        return this;
    }

    public R data(Object data) {
        this.data = data;
        return this;
    }

    public R message(String message) {
        this.message = message;
        return this;
    }

    // Getter 和 Setter 方法
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
